package com.yhxd.modular.news.controller;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.yhxd.modular.institutions.entity.Dept;
import com.yhxd.modular.news.entity.Attachment;
import com.yhxd.modular.news.entity.Classify;
import com.yhxd.modular.news.entity.News;
import com.yhxd.modular.news.entity.QNews;
import com.yhxd.modular.news.service.AllInformationService;
import com.yhxd.modular.news.service.AttachmentService;
import com.yhxd.modular.news.service.ClassifyService;
import com.yhxd.modular.news.service.MyInformationService;
import com.yhxd.modular.system.vo.ResultVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping(value = "/my/info")
@Api
public class MyInformationController {

    @Autowired
    private MyInformationService myInfoService;
    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private AttachmentService attachmentService;

    /**
     * 跳转到列表页
     * @return
     */
    @GetMapping(value = "/list")
    @RequiresPermissions("all_info_list")
    public String list() {
        return "mng/myinfo/myinfo_list";
    }

    /**
     * 查询列表数据
     * @param page
     * @param limit
     * @param dto
     * @return
     */
    @PostMapping(value = "/find_info_list")
    @RequiresPermissions("my_info_list")
    @ResponseBody
    public ResultVo findList(Integer page, Integer limit, News dto) {
        try {
            QPageRequest pageRequest = QPageRequest.of(page - 1, limit, new OrderSpecifier(Order.DESC, QNews.news.createTime));
            Page<News> resourcePage = this.myInfoService.findAll(pageRequest, dto);
            return ResultVo.success(resourcePage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 获取信息分类
     * @param pid (消息大类可以不传)
     * @param claType (0信息类型，1信息小类)
     * @return
     */
    @GetMapping(value = "/find_info_classify")
    @RequiresPermissions("find_info_list")
    @ResponseBody
    public ResultVo findInfoClassify(Long pid,Integer claType) {
        try {
            List<Classify> classify = classifyService.findClassify(pid, claType);
            return ResultVo.success(classify);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }


    /**
     * 跳转到添加页面
     * @return
     */
    @GetMapping(value = "/add")
    @RequiresPermissions("news_add")
    public String add(Model model) {
        return "mng/myinfo/myinfo_add";
    }
    /**
     * 保存信息
     * @return
     */
    @PostMapping(value = "/save")
    @RequiresPermissions("news_add")
    @ResponseBody
    public ResultVo save(News news) {
        try {
            news.setStatus(0);
            news.setImportant(0);
            //news.setContent(URLDecoder.decode(news.getContent(), "utf-8"));
            return this.myInfoService.saveOrUpdate(news);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @GetMapping(value = "/edit/{id}")
    @RequiresPermissions("edit")
    public String toDetail(Model model, @PathVariable("id") Long id) {
        News news = this.myInfoService.findById(id);
        model.addAttribute("news", news);
        List<Classify> classify = classifyService.findClassify(null, 0);
        List<Classify> childClassify = classifyService.findClassify(news.getParentClassify().getId(), 1);
        //List<Attachment>  attachment =attachmentService.findAttachment(news.getId());
        model.addAttribute("classify", classify);
        model.addAttribute("childClassify", childClassify);

        return "mng/myinfo/myinfo_edit";
    }
    /**
     * 修改信息
     * @return
     */
    @PostMapping(value = "/update")
    @RequiresPermissions("news_edit")
    @ResponseBody
    public ResultVo update(News news) {
        try {
            //news.setContent(URLDecoder.decode(news.getContent(), "utf-8"));
            return this.myInfoService.saveOrUpdate(news);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 删除
     * @return
     */
    @GetMapping(value = "/del/{id}")
    @RequiresPermissions("allInfo_del")
    @ResponseBody
    public ResultVo del(@PathVariable("id") Long id) {
        try {
            this.myInfoService.deleteById(id);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 消息发布
     * @return
     */
    @GetMapping(value = "/release/{id}")
    @RequiresPermissions("myInfo_release")
    @ResponseBody
    public ResultVo release(@PathVariable("id") Long id) {
        try {
            this.myInfoService.release(id);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

}
